package com.neouton.crm.customer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.customer.entity.CustomerVO;
import com.neouton.crm.customer.service.CustomerService;
import com.neouton.crm.entity.SysUserEntity;
import com.neouton.crm.pm.entity.SalesProjectManageVO;
import com.neouton.crm.pm.service.ProjectMemberService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: crm
 * @Timestamp: 2022-11-17
 * @Author: WuJie
 * @Description: 客户信息相关接口
 */
@RestController
@RequestMapping("customer")
public class CustomerController {

    @Resource
    private CustomerService customerService;
    @Resource
    private ProjectMemberService projectMemberService;

    /**
     * 多条件分页查询客户信息列表
     * @Timestamp: 2022-11-18
     * @Author: WuJie
     * @param pageNum 当前页
     * @param pageSize 每页数量
     * @param customer 其余查询条件
     * @return 分页查询结果
     */
    @PostMapping("/getCustomerByPage/{pageNum}/{pageSize}/{orderBy}")
    public ResponseAPI getCustomerByPage(@PathVariable(value = "pageNum") int pageNum,
                            @PathVariable(value = "pageSize") int pageSize,
                            @RequestBody CustomerVO customer,@PathVariable(value = "orderBy") String orderBy,@RequestHeader("token") String token) {
        IPage<CustomerVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<CustomerVO> result = customerService.queryCustomerByPage(page, customer,orderBy,token);
        return ResponseAPI.successData(result);
    }


    /**
     * 获取客户待办列表
     * @param pageNum
     * @param pageSize
     * @param customer
     * @param orderBy
     * @param token
     * @return
     */
    @PostMapping("/getCustomerToDoList/{pageNum}/{pageSize}/{orderBy}")
    public ResponseAPI getCustomerToDoList(@PathVariable(value = "pageNum") int pageNum,
                                         @PathVariable(value = "pageSize") int pageSize,
                                         @RequestBody CustomerVO customer,@PathVariable(value = "orderBy") String orderBy,@RequestHeader("token") String token) {
        IPage<CustomerVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<CustomerVO> result = customerService.getCustomerToDoList(page, customer,orderBy,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 获取客户已办列表
     * @param pageNum
     * @param pageSize
     * @param customer
     * @param orderBy
     * @param token
     * @return
     */
    @PostMapping("/getCustomerDoneList/{pageNum}/{pageSize}/{orderBy}")
    public ResponseAPI getCustomerDoneList(@PathVariable(value = "pageNum") int pageNum,
                                           @PathVariable(value = "pageSize") int pageSize,
                                           @RequestBody CustomerVO customer,@PathVariable(value = "orderBy") String orderBy,@RequestHeader("token") String token) {
        IPage<CustomerVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<CustomerVO> result = customerService.getCustomerDoneList(page, customer,orderBy,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 获取客户申请记录列表
     * @param pageNum
     * @param pageSize
     * @param customer
     * @param orderBy
     * @param token
     * @return
     */
    @PostMapping("/getCustomerRecordsList/{pageNum}/{pageSize}/{orderBy}")
    public ResponseAPI getCustomerRecordsList(@PathVariable(value = "pageNum") int pageNum,
                                           @PathVariable(value = "pageSize") int pageSize,
                                           @RequestBody CustomerVO customer,@PathVariable(value = "orderBy") String orderBy,@RequestHeader("token") String token) {
        IPage<CustomerVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<CustomerVO> result = customerService.getCustomerRecordsList(page, customer,orderBy,token);
        return ResponseAPI.successData(result);
    }
    /**
     * 通过客户名称搜索,对外接口
     * @param customer
     * @return
     */
    @PostMapping("/selectCustomerByEnterpriseId")
    public ResponseAPI selectCustomerByEnterpriseId(@RequestBody CustomerVO customer,@RequestHeader("token") String token) {
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"customer", token);
        return ResponseAPI.successData(customerService.selectCustomerByEnterpriseId(customer,userList));
    }

    /**
     * 通过客户名称搜索,对外接口
     * @return
     */
    @PostMapping("/selectAllCustomers")
    public ResponseAPI selectAllCustomers() {
        return ResponseAPI.successData(customerService.selectAllCustomers());
    }

    /**
     * 通过客户Id搜索,对外接口
     * @param customerId
     * @return
     */
    @PostMapping("/queryCustomerById/{customerId}")
    public ResponseAPI queryCustomerById(@PathVariable("customerId") Integer customerId,@RequestHeader("token") String token) {
        return ResponseAPI.successData(customerService.queryCustomerById(customerId));
    }

    /**
     * 通过客户名称搜索,对外接口，需要更改
     * @param customer
     * @return
     */
    @PostMapping("/selectCustomerByCustomerName")
    public ResponseAPI selectCustomerByCustomerName(@RequestBody CustomerVO customer) {
        return ResponseAPI.successData(customerService.selectCustomerByCustomerName(customer.getCustomerName()));
    }
    /**
     * 新增或更新客户信息
     * @Timestamp: 2022-11-22
     * @Author: WuJie
     * @param customer 需更新实体
     * @return 成功失败
     */
    @PostMapping("/saveOrUpdateCustomer")
    public ResponseAPI saveOrUpdateCustomer(@RequestBody CustomerVO customer,@RequestHeader("token") String token) {
        Boolean data = customerService.saveOrUpdateCustomer(customer,token);
        return ResponseAPI.successData(data);
    }

    /**
     * 禁用客户信息
     * @Timestamp: 2022-11-22
     * @Author: WuJie
     * @param customerId 需删除的ID
     * @return 成功失败
     */
    @PostMapping("/deleteCustomer/{customerId}")
    public ResponseAPI deleteCustomer(@PathVariable(value = "customerId") Integer customerId){
        Boolean flage = customerService.deleteCustomerById(customerId);
        return ResponseAPI.successData(flage);
    }

    /**
     * 通过审批日志ID改变客户状态：禁用客户信息
     * @param approvalId
     * @return
     */
    @PostMapping("/forbiddenCustomerByApplyId/{approvalId}")
    public ResponseAPI forbiddenCustomerByApplyId(@PathVariable(value = "approvalId") Integer approvalId){
        Boolean flage = customerService.forbiddenCustomerByApplyId(approvalId);
        return ResponseAPI.successData(flage);
    }


    /**
     * 启用客户信息
     * @Timestamp: 2022-11-22
     * @Author: WuJie
     * @param customerId 需删除的ID
     * @return 成功失败
     */
    @PostMapping("/enableCustomer/{customerId}/{backUserId}")
    public ResponseAPI enableCustomer(@PathVariable(value = "customerId") Integer customerId,
                                      @PathVariable(value = "backUserId") Integer backUserId){
        Boolean flage = customerService.enableCustomerById(customerId,backUserId);
        return ResponseAPI.successData(flage);
    }
    /**
     * 根据客户名称查询客户信息是否存在
     * @Timestamp: 2022-11-18
     * @Author: WuJie
     * @param customer 客户信息
     * @return true存在，false不存在
     */
    @PostMapping("/queryCustomerExit")
    public ResponseAPI queryCustomerExit(@RequestBody CustomerVO customer){
        CustomerVO customers = customerService.queryCustomerExit(customer);
        return ResponseAPI.successData(customers);
    }

    /**
     * 审批通过
     * @param customer
     * @return
     */
    @PostMapping("/approveCustomer")
    public ResponseAPI approveCustomer(@RequestBody CustomerVO customer,@RequestHeader("token") String token){
        Boolean flage = customerService.approveCustomer(customer,token,false);
        return ResponseAPI.successData(flage);
    }


    @PostMapping("/queryTodoCustomerByApprovalId/{approvalId}")
    public ResponseAPI queryTodoCustomerByApprovalId(@PathVariable("approvalId") Integer approvalId){
        return ResponseAPI.successData( customerService.queryTodoCustomerByApprovalId(approvalId));
    }

    @PostMapping("/queryHistoryCustomerByApprovalId/{approvalId}")
    public ResponseAPI queryHistoryCustomerByApprovalId(@PathVariable("approvalId") Integer approvalId){
        return ResponseAPI.successData( customerService.queryHistoryCustomerByApprovalId(approvalId));
    }
    /**
     * 审批驳回
     * @param customer
     * @return
     */
    @PostMapping("/rejectionCustomer")
    public ResponseAPI rejectionCustomer(@RequestBody CustomerVO customer,@RequestHeader("token") String token){
        Boolean flage = customerService.rejectionCustomer(customer,token);
        return ResponseAPI.successData(flage);
    }
    /**
     * 查询并更改是否为供应商，每次刷新更新
     * @return true成功，false失败
     */
    @PostMapping("/selectIsSupplier")
    public ResponseAPI selectIsSupplier() {
        Boolean flage =customerService.selectIsSupplier();
        return ResponseAPI.successData(true);
    }

    /**
     * （铃铛）待办事项查询
     * @param workId
     * @return
     */
    @PostMapping("/queryCustomerApprovalByWorkId/{workId}")
    public ResponseAPI queryCustomerApprovalByWorkId(@PathVariable("workId") Integer workId){
        return ResponseAPI.successData( customerService.queryCustomerApprovalByWorkId(workId));
    }

    /**
     * 查询所有的供应商名称
     * @return
     */
    @PostMapping("/selectAllEnterprise")
    public ResponseAPI selectAllEnterprise() {
        List<CustomerVO> ct = customerService.selectAllEnterprise();
        return ResponseAPI.successData(ct);
    }



    @PostMapping("/exportCustomerInfo")
    public void customerInfosExport(HttpServletResponse response, @RequestBody CustomerVO customer,@RequestHeader("token") String token) throws Exception{
        customerService.exportCustomerInfo(response,customer,token);
    }


    /**
     * 依据客户ID获取客户相关信息
     * @param customerId
     * @return
     */
    @PostMapping("/queryCustomerCorrelationByCustomerID/{customerId}")
    public ResponseAPI queryProjectByID(@PathVariable(value = "customerId") int customerId,@RequestHeader("token") String token) {
        CustomerVO customerCorrelation = customerService.queryCustomerCorrelationByCustomerID(customerId,token);
        return ResponseAPI.successData(customerCorrelation);
    }


    /**
     * 客户转移
     * @param userId 转移目标ID
     * @param customer 转移具体内容
     * @return
     */
    @PostMapping("/transferCustomer/{userId}")
    public ResponseAPI transferCustomer(@PathVariable(value = "userId") Integer userId,@RequestBody List<CustomerVO> customerList) {
        return ResponseAPI.successData(customerService.transferCustomer(userId,customerList));
    }


    /**
     * 通过客户ID查询客户视图信息接口
     * @param customerId
     * @param token
     * @return
     */
    @PostMapping("/getCustomerDocumentaryViewInfo/{customerId}")
    public ResponseAPI getCustomerDocumentaryViewInfo(@PathVariable(value = "customerId") Integer customerId,@RequestHeader("token") String token) {
        return ResponseAPI.successData(customerService.getCustomerDocumentaryViewInfo(customerId,token));
    }
}
